package com.mini.grpc.client;

import com.mini.grpc.client.netty.NettyChannelBuilder;

import java.net.InetSocketAddress;
import java.net.SocketAddress;

/**
 * ManagedChannelBuilder提供了创建ManagedChannel的静态方法。
 */
public abstract class ManagedChannelBuilder<T extends ManagedChannelBuilder<T>> extends ChannelBuilder<T> {
    
    /**
     * 创建一个新的ManagedChannelBuilder，用于连接指定的主机和端口
     *
     * @param host 主机名
     * @param port 端口号
     * @return 新的ManagedChannelBuilder
     */
    public static ManagedChannelBuilder<?> forAddress(String host, int port) {
        return new NettyChannelBuilder(new InetSocketAddress(host, port));
    }
    
    /**
     * 创建一个新的ManagedChannelBuilder，用于连接指定的地址
     *
     * @param address 套接字地址
     * @return 新的ManagedChannelBuilder
     */
    public static ManagedChannelBuilder<?> forAddress(SocketAddress address) {
        return new NettyChannelBuilder(address);
    }
    
    /**
     * 创建一个新的ManagedChannelBuilder，用于连接指定的目标
     *
     * @param target 目标地址，格式为"host:port"
     * @return 新的ManagedChannelBuilder
     */
    public static ManagedChannelBuilder<?> forTarget(String target) {
        String[] parts = target.split(":");
        if (parts.length != 2) {
            throw new IllegalArgumentException("Target must be in the format 'host:port'");
        }
        
        String host = parts[0];
        int port;
        try {
            port = Integer.parseInt(parts[1]);
        } catch (NumberFormatException e) {
            throw new IllegalArgumentException("Port must be a number", e);
        }
        
        return forAddress(host, port);
    }
} 